import {commonApi} from "@/api";
import sessionStorage from "@/utils/session-storage";
const DictKey = "dicts";

export async function getDictName(dictType, value){
  const res = await commonApi.queryDict(dictType)
  if (res.data[dictType]){
    for (let item of res.data[dictType]){
      if (item.dictValue === value){
        return item.remark
      }
    }
  }
}

export async function getDicts(dictType){
  let dicts = sessionStorage.getItem(DictKey) || {}
  let noExistKey = []
  for (let key of dictType.split(",")){
    if (!dicts[key]){
      noExistKey.push(key)
    }
  }
  if (noExistKey.length === 0){
    return dicts
  }
  const type = noExistKey.join()
  const res = await commonApi.queryDict(type)
  for (let key in res.data){
    if (!dicts[key]){
      dicts[key] = {}
    }
    for (let item of res.data[key]){
      dicts[key][item.dictValue] = item.remark
    }
  }
  sessionStorage.setItem(DictKey, dicts)
  return dicts
}
